package com.wzy.level.user.userServlet;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import com.wzy.Jdbc封装.pojo.News_detail;
import com.wzy.Jdbc封装.pojo.News_user;
import com.wzy.level.detail.detailService.detailService;
import com.wzy.level.detail.detailServiceImpl.detailServiceImpl;
import com.wzy.level.detail.detailServlet.detail;
import com.wzy.level.user.userService.userService;
import com.wzy.level.user.userServiceImpl.userServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@WebServlet("/user_form_mid")
public class user_form_insert_update extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = Integer.parseInt(req.getParameter("id"));
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        int usertype = Integer.parseInt(req.getParameter("usertype").trim());

        News_user news_user = new News_user();
        news_user.setUserName(username);
        news_user.setPassword(password);
        news_user.setEmail(email);
        news_user.setUserType(usertype);

        int state = Integer.parseInt(req.getParameter("state"));

        userService userService = new userServiceImpl();
        if(state ==0){
            news_user.setId(id);
            int update = userService.update(news_user);
            if (update>0){
//                resp.sendRedirect(req.getContextPath()+"/pageManage");
                resp.sendRedirect("http://localhost:8080//pageManage?cate=2");
            }else {
                System.out.println( "修改失败");
            }
        }else if(state == 1){
            int insert = userService.insert(news_user);
            if(insert>0){
//                resp.sendRedirect(req.getContextPath()+"/pageManage");
                resp.sendRedirect("http://localhost:8080//pageManage?cate=2");
            }else {
                System.out.println( "增加失败");
            }
        }
    }
}
